//
//  MYShopSalesOrderPayView.swift
//  MuYing
//
//  Created by tbb on 2019/7/31.
//  Copyright © 2019 TBB. All rights reserved.
//

import UIKit

class MYShopSalesOrderPayView: MYSelectBaseView,UITableViewDataSource,UITableViewDelegate {
    //typealias PayTypeClickedAction = (() -> Void);
    typealias ConfirmClickedAction = ((Int,String) -> (Void));
    typealias CancelClickedAction = (() -> Void);
    
    @objc var index:Int = 0;
    @objc var typeStr = kYQLSSwift("支付宝");
    
    var cancelClickedAction:CancelClickedAction?
    @objc var confirmClickedAction:ConfirmClickedAction?
//    var payTypeClickedAction:PayTypeClickedAction?
    
    let confirmPayViewH:CGFloat = 290.0;
    lazy var confirmPayView: MYShopSalesOrderConfirmPayView = {
        let confirmPayView = MYShopSalesOrderConfirmPayView(frame: CGRect(x: 0, y: kSwiftkScreenSizeHeight+confirmPayViewH, width: kSwiftkScreenSizeWidth, height: confirmPayViewH));
        self.addSubview(confirmPayView);
        confirmPayView.confirmBtn.setTitle(kYQLSSwift("立即付款"), for: .normal);
        confirmPayView.typePayBtn.setTitle(typeStr, for: .normal);
        confirmPayView.priceLb.text = "0.001";
        confirmPayView.titleLb.text = kYQLSSwift("确定付款");
        confirmPayView.confirmBtn.addTarget(self, action: #selector(confirmAction(_:)), for: .touchUpInside);
        confirmPayView.cancelBtn.addTarget(self, action: #selector(cancelSelctTableView), for: .touchUpInside);
        let tap = UITapGestureRecognizer(target: self, action: #selector(payTypeAction));
        confirmPayView.bgView.addGestureRecognizer(tap);
        return confirmPayView
    }()
    let iconArr = ["pay_type_Alipay_iocn","Login_wechat_icon"];
    /*
     ,"pay_type_wechat_iocn","pay_type_paypal_iocn","pay_type_payoffline_iocn","pay_type_bankcard_iocn"
     
     ,kYQLSSwift("微信"),kYQLSSwift("paypal"),kYQLSSwift("线下支付"),kYQLSSwift("线上银行转账")
     
     */
    let titleArr = [kYQLSSwift("支付宝"),kYQLSSwift("微信")];
    lazy var typePayView: MYShopSalesOrderBasePayView = {
        let typePayView = MYShopSalesOrderBasePayView(frame: CGRect(x: 0, y: kSwiftkScreenSizeHeight+confirmPayViewH, width: kSwiftkScreenSizeWidth, height: confirmPayViewH));
        typePayView.titleLb.text = kYQLSSwift("选择付款方式");
        typePayView.confirmBtn.setTitle(kYQLSSwift("立即付款"), for: .normal);
        typePayView.confirmBtn.addTarget(self, action: #selector(confirmAction(_:)), for: .touchUpInside);
        typePayView.cancelBtn.addTarget(self, action: #selector(cancelSelctType), for: .touchUpInside);
        self.addSubview(typePayView);
        typePayView.addSubview(self.selectTableView);
        self.selectTableView.register(UINib.init(nibName: "MYShopSalesOrderPayTypeCell", bundle: nil), forCellReuseIdentifier: "MYShopSalesOrderPayTypeCell");
        self.selectTableView.mas_makeConstraints({ (make) in
            make?.top.mas_equalTo()(typePayView.topLineView.mas_bottom)?.mas_equalTo()(0);
            make?.left.mas_equalTo()(0);
            make?.right.mas_equalTo()(0);
            make?.bottom.mas_equalTo()(-68);
        });
        return typePayView
    }()
    //选择支付方式
    @objc func payTypeAction() {
        self.typePayView.isHidden = false;
        UIView.animate(withDuration: 0.3) {
            self.typePayView.top = kSwiftkScreenSizeHeight-self.confirmPayViewH-kHeight_Botton_SafeArea;
        };
    }
    //取消选择支付类型
    @objc func cancelSelctType() {
        UIView.animate(withDuration: 0.3, animations: {
            self.typePayView.top = kSwiftkScreenSizeHeight+self.confirmPayViewH;
        }) { (finish) in
            self.typePayView.isHidden = true;
        }

    }
    //立即支付
    @objc func confirmAction(_ sender:UIButton) {
        self.cancelSelctTableViewAction();
        if let confirmClickedAction = self.confirmClickedAction {
            confirmClickedAction(index,typeStr);
        }
    }
    @objc public class func shopSalesOrderPayView (_ totalAmount:String , _ confirmBlock: @escaping ConfirmClickedAction,_ cancelBlock: @escaping CancelClickedAction) {
        let payView = MYShopSalesOrderPayView(frame: UIScreen.main.bounds);
        payView.backgroundColor = UIColorFromHex(rgbValue: 0x000000).withAlphaComponent(0.2);
        //payView.payTypeClickedAction = payTypeBlock;
        payView.confirmPayView.priceLb.text = totalAmount;
        payView.confirmClickedAction = confirmBlock;
        payView.cancelClickedAction = cancelBlock;
        payView.showSelctTableView();
    }
    
    override func showSelctTableView() {
        
        //1. 获取当前应用的主窗口
        let keyWindow = UIApplication.shared.keyWindow;
        keyWindow?.addSubview(self);
        UIView.animate(withDuration: 0.3) {
            self.confirmPayView.top = kSwiftkScreenSizeHeight-self.confirmPayViewH-kHeight_Botton_SafeArea;
        };
    }
    
    override func cancelSelctTableView() {
//        self.cancelClickedAction ?? <#default value#>();
        if let cancelClickedAction =  cancelClickedAction {
            cancelClickedAction();
        }
        cancelSelctTableViewAction();
    }
    
    func cancelSelctTableViewAction() {
        UIView.animate(withDuration: 0.3, animations: {
            if !self.typePayView.isHidden {
                self.typePayView.top = kSwiftkScreenSizeHeight+self.confirmPayViewH;
            }
            self.confirmPayView.top = kSwiftkScreenSizeHeight+self.confirmPayViewH;
        }) { (finished) in
            self.removeFromSuperview();
        }
    }

}

extension MYShopSalesOrderPayView {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.iconArr.count;
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "MYShopSalesOrderPayTypeCell") as? MYShopSalesOrderPayTypeCell else { return UITableViewCell() }
        cell.paytypeIcon.image = UIImage(named: self.iconArr[indexPath.row]);
        cell.titleLb.text = self.titleArr[indexPath.row];
        return cell;
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1;
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44;
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        index = indexPath.row;
        typeStr = self.titleArr[indexPath.row];
        confirmPayView.typePayBtn.setTitle(typeStr, for: .normal);
        confirmPayView.typePayBtn.titleLabel?.sizeToFit();
        confirmPayView.typePayBtn.sizeToFit();
        self.selectTableView.reloadData();
        self.cancelSelctType();
    }
}
